package com.keshan.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author keshan
 * @date 2022/10/20 - 18:37
 */
public class Msg {
    //状态码 100成功 200失败
//    唐铮20230210：建议把除了100和200以外的方法全删了，太冗余了
    private int code;
    //提示信息
    private String msg;
    //用户返回的数据
    private Map<String,Object> extend = new HashMap<String, Object>();

    /**
     * @author tangzheng
     * @date 2023/2/10 18:50
     * @description 无参的构造方法
     * @return com.keshan.utils.Msg
     */
    public static Msg success(){
        Msg result = new Msg();
        result.setCode(100);
        result.setMsg("处理成功");
        return result;
    }

    /**
     * @author tangzheng
     * @date 2023/2/10 18:50
     * @description 无参的构造方法
     * @return com.keshan.utils.Msg
     */
    public static Msg fail(){
        Msg result = new Msg();
        result.setCode(200);
        result.setMsg("处理失败");
        return result;
    }

    /**
     * @author tangzheng
     * @date 2023/2/10 18:50
     * @description 创建时设置msg字段
     * @param msg 返回的错误消息
     * @return com.keshan.utils.Msg
     */
    public static Msg fail(String msg){
        Msg result = new Msg();
        result.setCode(200);
        result.setMsg(msg);
        return result;
    }

    public static Msg successRegister(){
        Msg result = new Msg();
        result.setCode(101);
        result.setMsg("注册成功");
        return result;
    }
    public static Msg successLogin(){
        Msg result = new Msg();
        result.setCode(102);
        result.setMsg("认证成功");
        return result;
    }

    public static Msg failInsert(){
        Msg result = new Msg();
        result.setCode(206);
        result.setMsg("设备无法匹配用户");
        return result;
    }

    public static Msg failLogin(){
        Msg result = new Msg();
        result.setCode(201);
        result.setMsg("账号或密码错误，请重试或者注册");
        return result;
    }

    public static Msg failRegister(){
        Msg result = new Msg();
        result.setCode(302);
        result.setMsg("注册插入失败，请重试");
        return result;
    }

    public Msg add(String key,Object value){
        this.getExtend().put(key,value);
        return this;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Map<String, Object> getExtend() {
        return extend;
    }

    public void setExtend(Map<String, Object> extend) {
        this.extend = extend;
    }

    @Override
    public String toString() {
        return "Msg{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", extend=" + extend +
                '}';
    }

}
